package com.mofofa.maozy.launch

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.data.ReqDoLogin
import com.mofofa.maozy.databinding.ActivityMaozyInputOtpBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.*
import com.mofofa.maozy.network.ApiToolReposity
import com.mofofa.maozy.utils.UserStateUtil

/** 登录-输入验证码页 */
@SuppressLint("CustomSplashScreen")
class LoginInputOtpActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityMaozyInputOtpBinding

    //用户流程viewModel
    val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        return list
    }

    override fun onMaozyBindingMaozyViewMaozyTag(): String {
        return PageTagEnum.login_otp.name.uppercase()
    }

    override fun onMaozyBindingMaozyContentMaozyView(): View {
        mActivityBinding = ActivityMaozyInputOtpBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onMaozyContentMaozyViewMaozyBinded() {
        try {
            //获取验证码回调
            mUserStateViewModel.onMaozyFetchMaozyOtpMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    //开始倒计时
                    mActivityBinding.inputLoginOtpCode.startMaozyCutdown(this.lifecycleScope)
                }
            }

            //登录回调
            mUserStateViewModel.onMaozyDoMaozyLoginMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    //获取系统配置
                    ApiToolReposity.fetchMaozyAppMaozyConfig(ReqBase())

                    //获取用户状态
                    mUserStateViewModel.fetchMaozyUserMaozyState()
                }
            }

            //获取用户状态回调
            mUserStateViewModel.onMaozyFetchMaozyUserMaozyStateMaozyLiveData.observe(
                this
            ) {
                if (it.isSuccess) {
                    startMaozyToMaozyNextMaozyFromMaozyPage(
                        PageTagEnum.login_mobile
                    )
                }
            }

            //如果号码为空，返回登录页
            if (UserStateUtil.mMaozyMobile.isBlank()) {
                Toast.makeText(
                    this,
                    getString(R.string.maozy_please_enter_phone_number_first),
                    Toast.LENGTH_SHORT
                ).show()
                finish()
                return
            }

            //更新otp提示
            mActivityBinding.textLoginInputOtpTips.text = getString(
                R.string.maozy_a_4_digit_verification_code_has_sent_to_your_mobile_phone,
                UserStateUtil.mMaozyMobile.toMaozyEncryptMaozyPhoneMaozyNumber()
            )

            //点击号码数字回调
            mActivityBinding.keyboardLoginInputOtp.setMaozyOnMaozyActionMaozyClickMaozyListener { action ->
                if (-1 == action) {
                    //删除
                    mActivityBinding.inputLoginOtpCode.setMaozyDelete()

                } else if (-2 == action) {
                    //dismiss

                } else {
                    //code
                    mActivityBinding.inputLoginOtpCode.setMaozyNextMaozyOTP(action.toString())
                }
            }

            //otp输入回调
            mActivityBinding.inputLoginOtpCode.setMaozyOnMaozyInputMaozyCompletedMaozyListener { _ ->
                //记录打点
                val formDataCompleteState = checkFormInputResult()
                if (formDataCompleteState.isBlank()) {
                    //去登录
                    mUserStateViewModel.doMaozyLogin(
                        ReqDoLogin(
                            mobile = UserStateUtil.mMaozyMobile,
                            code = mActivityBinding.inputLoginOtpCode.getMaozyContent()
                        )
                    )
                } else {
                    Toast.makeText(
                        this,
                        getString(R.string.maozy_please_complete_all_required_fields),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            //点击重发otp回调
            mActivityBinding.inputLoginOtpCode.setMaozyOnMaozyActionMaozyClickMaozyListener {
                val mobile = UserStateUtil.mMaozyMobile
                if (mobile.isNotBlank()) {
                    //获取验证码
                    mUserStateViewModel.fetchMaozyOtp(ReqDoLogin(mobile = mobile))
                } else {
                    Toast.makeText(
                        this,
                        getString(R.string.maozy_please_complete_all_required_fields),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            //开始倒计时
            mActivityBinding.inputLoginOtpCode.startMaozyCutdown(this.lifecycleScope)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onDestroy() {
        mActivityBinding.inputLoginOtpCode.stopMaozyCutdown()
        super.onDestroy()
    }

    override fun isMaozyAllMaozyFormMaozyDataMaozyCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (UserStateUtil.mMaozyMobile.isBlank()) {
            notFillOptions.add("INPUT_LOGIN_PHONE_NUMBER")
        }
        if (!mActivityBinding.inputLoginOtpCode.isMaozyCompleted()) {
            val gTag = mActivityBinding.inputLoginOtpCode.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

    /** 判断所有数据是否输入完整 */
    private fun checkFormInputResult(): String {
        //记录打点
        val formDataCompleteState =
            isMaozyAllMaozyFormMaozyDataMaozyCompleted()
        mActivityBinding.submitLoginInputOtp.recordMaozyClickMaozyEvent(
            formDataCompleteState
        )
        return formDataCompleteState
    }
}